package com.samskievert.cactusboom;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;

import com.threerings.media.sprite.Sprite;
import java.awt.geom.AffineTransform;

/**
 * Displays a piece on the board view.
 */
public class PickupSprite extends Sprite
{
    /** The dimensions of our sprite in pixels. */
    public static final int SIZE = 48;

    boolean pickedUp = false;
    
    private CBObject.Pickup _pickup;
    private CBObject _gameobj;
    
    /**
     * Creates a piece sprite to display the supplied game piece.
     */
    public PickupSprite (CBObject.Pickup pickup, CBObject gameobj)
    {
        super(SIZE, SIZE);
        System.out.println("PickupSprite created!");
        _pickup = pickup;
        _gameobj = gameobj;
        updatePickup();
    }

    /**
     * This piece never really updates itself...
     * but I'll keep this around for now.
     */
    public void updatePickup() 
    {
        // set our location based on the location of the piece
        setLocation(_pickup.x * SIZE, _pickup.y * SIZE);

        // force a redraw in case our color changed but not our location
        invalidate();
    }
    
    @Override //from Sprite
    public void tick(long tickStamp)
    {
        super.tick(tickStamp);
        
        if(!pickedUp)
        {
            ArrayList<CBObject.Cowboy> playerHits = new ArrayList<CBObject.Cowboy>();
            Iterator p = _gameobj.cowboys.iterator();
            while(p.hasNext()){
              CBObject.Cowboy cowboy = (CBObject.Cowboy)p.next();
              if(cowboy.x == _pickup.x && cowboy.y == _pickup.y)
              {
                  System.out.println("Someone grabbed me at (" +  _pickup.x + "," + _pickup.y + ")");
                  pickedUp = true;
                  _gameobj.manager.invoke("youGotTheGold",_pickup);
              }
            }
            
        }
    }
    
    @Override // from Sprite
    public void paint (Graphics2D gfx)
    {
       
       int px = _bounds.x + 12, py = _bounds.y + 12;
       int pwid = _bounds.width - 24, phei = _bounds.height - 24;
       gfx.setColor(Color.yellow);
       gfx.fillOval(px,py,8,8);
       gfx.fillOval(px+5,py+10,12,12);
       gfx.fillOval(px+12,py+2,10,10);
       gfx.fillOval(px+20,py+10,6,6);
    }

}

